import java.util.LinkedList;
import java.util.Queue;

public class Solution1 {
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        int prev = image[sr][sc]; // 统计刚开始的颜色
        int[] dx = {0, 0, 1, -1};
        int[] dy = {1, -1, 0, 0};
        int m = image.length;
        int n = image[0].length;
        if(prev == color) {
            return image;
        }
        Queue<int[]> q = new LinkedList<>();
        q.add(new int[]{sr, sc});

        while(!q.isEmpty()) {
            int[] t = q.poll();
            int a = t[0];
            int b = t[1];
            image[a][b] = color;
            for(int k = 0; k < 4; k++) {
                int x = a + dx[k];
                int y = b + dy[k];
                if(x >= 0 && x < m && y >= 0 && y < n && image[x][y] == prev) {
                    q.add(new int[]{x, y});
                }
            }
        }
        return image;
    }
}
